import {
    Platform,
    Dimensions,
    StatusBar,
    PermissionsAndroid,
    CameraRoll,
} from "react-native"
import RNFS from 'react-native-fs';

export const deviceWidth = Dimensions.get("window").width // 设备的宽度
export const deviceHeight = Dimensions.get("window").height // 设备的高度

//UI设计图的宽度
const designWidth = 750
//UI设计图的高度
const designHeight = 1334

export const unitWidth = deviceWidth / designWidth
//计算手机屏幕高度对应设计图高度的单位高度
export const unitHeight = deviceHeight / designHeight

export const statusBarHeight = getStatusBarHeight();
//标题栏的高度
export const titleHeight = unitWidth * 100 + statusBarHeight;

const X_WIDTH = 375
const X_HEIGHT = 812
const XR_WIDTH = 414
const XR_HEIGHT = 896

export function getStatusBarHeight() {
    if (Platform.OS == 'android') return StatusBar.currentHeight;
    if (isIPhoneX()) {
        return 44
    }
    return 20
}

export function isIPhoneX() {
    return (
        Platform.OS === "ios" &&
        ((deviceHeight === X_HEIGHT && deviceWidth === X_WIDTH) ||
            (deviceHeight === XR_HEIGHT && deviceWidth === XR_WIDTH))
    )
}

export function reallySize(size) {                                  //真实尺寸
    return deviceWidth / 375 * size;
}

export function checkPhone(phone) {
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
        return false
    }
    return true
}

export function getRandomColor() {
    return "rgb(" + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 10) + ')'
}


export function changeId(id) {
    if (id.length === 18) {
        var id1 = id.substring(0, 6)
        var id2 = '********'
        var id3 = id.substring(14, 18)
        return id1 + id2 + id3
    }
    return id
}

export function pureBankData(data) {
    var pickData = []
    data.forEach(function (item) {
        pickData.push(item.name)
    })
    return pickData
}

export function getBankId(sourceData, selected) {
    let id = ''
    if (selected[0]) {
        sourceData.some(item => {
            if (item.name === selected[0]) {
                id = item.id
                return true
            }
        })
    }
    return id
}

export function pureAreaData(data) {
    var pickData = []
    data.forEach(function (item) {
        var object = {}
        var citys = item.city.map(function (city) {
            return city.name
        })
        if (citys.length === 0) {
            citys = [item.name]
        }
        object[item.name] = citys
        pickData.push(object)
    })
    return pickData
}


export function getPovinceAndCityId(sourceData, selected) {
    if (selected.length !== 2 || !sourceData) {
        return ['', '']
    }
    var province = selected[0]
    var city = selected[1]
    var provinceId = '',
        cityId = ''
    var citys = []
    sourceData.some(function (item) {
        if (item.name === province) {
            citys = item.city
            provinceId = item.id
            return true
        }
    })
    citys.some(function (item) {
        if (item.name === city) {
            cityId = item.id
            return true
        }
    })
    return [provinceId, cityId]
}


export function getPovinceAndCityName(sourceData, selected) {
    if (selected.length !== 2 || !sourceData) {
        return ['', '']
    }
    var province = selected[0]
    var city = selected[1]
    var provinceName = '',
        cityName = ''
    var citys = []
    sourceData.some(function (item) {
        if (item.id == province) {
            citys = item.city
            provinceName = item.name
            return true
        }
    })
    citys.some(function (item) {
        if (item.id == city) {
            cityName = item.name
            return true
        }
    })
    return [provinceName, cityName]
}

export function transformCardData(data) {
    var arr = data.split('/')
    return '20' + arr[1] + '-' + arr[0] + '-' + '01'
}






